using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace CollisionTool
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            // set member variable defaults
            m_nLastSavedBoxIndex = 0;

            // super save intialization
            fSaverDlg = new SuperSaver(this);

            InitializeComponent();
        }

        private void openToolStripMenuItem_Click(object sender, EventArgs e)
        {
            OpenFileDialog fdlg = new OpenFileDialog();
            fdlg.Title = "Open Image File Dialog";
            fdlg.InitialDirectory = @"c:\";
            fdlg.Filter = "PNG (*.png)|*.png|All files(*.*)|*.*";
            fdlg.FilterIndex = 2;
            fdlg.RestoreDirectory = true;
            if (fdlg.ShowDialog() == DialogResult.OK)
            {
                m_sFilename = fdlg.FileName;
                MyBitmap = new Bitmap(m_sFilename);
            }
        }

        private void saveToolStripMenuItem_Click(object sender, EventArgs e)
        {
            //string sFolderLoc;

            //SaveFileDialog fdlg = new SaveFileDialog();
            //fdlg.Title = "Save Image File Dialog";
            //fdlg.InitialDirectory = @"c:\";
            //fdlg.Filter = "BOB (*.bob)|*.bob|All files(*.*)|*.*";
            //fdlg.FilterIndex = 2;
            //fdlg.RestoreDirectory = true;

            //if (fdlg.ShowDialog() == DialogResult.OK)
            //{
                // grab the new folder location
                //sFolderLoc = fdlg.FileName;

                // grab just the root folder location
                //string[] sTempSplit = new string[2];
                //sTempSplit = sFolderLoc.Split('.');
                //int nLastSlash = sTempSplit[0].LastIndexOf('\\');
                //sFolderLoc = sTempSplit[0].Remove(nLastSlash);
            
                // grab the original image's name (no extension or folder)
                string[] sTempSplit = new string[2];
                sTempSplit = m_sFilename.Split('.');

                m_sBobFilename = sTempSplit[0] + ".bob";
            //}
            fSaverDlg.Show();
        }

        public void saveFileOut(int nFramesWide, int nFramesHigh, int nNumFrames)
        {
            // we'll need to find out how wide and high each of the frames are
            // individually based on the image size
            int nFrameWidth = MyBitmap.Width / nFramesWide;
            int nFrameHeight = MyBitmap.Height / nFramesHigh;

            // create our new collision object to be saved out
            BumblePig.CollisionObject ColObj = new BumblePig.CollisionObject();

            // create the list of our animation frames based on user input
            ColObj.AddNumCollisionFrames(nNumFrames);

            // loop through our global list of collision boxes
            // - check to see which frame they are contained within
            // - add them to the appropriate frame
            for (int nBoxIndex = 0; nBoxIndex < m_lCollisionBoxes.Count; ++nBoxIndex)
            {
                int nXOffset = 0;
                int nYOffset = 0;
                int nFrameIndex = 0;
                // loop through nFramesHigh worth of frames in the Y direction
                for (int nYFrame = 0; nYFrame < nFramesHigh; ++nYFrame)
                {
                    // loop through nFramesWide worth of frames in the X direction
                    for (int nXFrame = 0; nXFrame < nFramesWide; ++nXFrame)
                    {
                        // grab the current box for ease
                        BumblePig.CollisionBox colBox = m_lCollisionBoxes[nBoxIndex];

                        // create temporary collision box for frame so we can use
                        // our Contains() function (for ease)
                        BumblePig.CollisionBox colBoxFrame = new BumblePig.CollisionBox();
                        colBoxFrame.posMin = new BumblePig.Vec2(nXOffset, nYOffset);
                        colBoxFrame.posMax = new BumblePig.Vec2(nXOffset + nFrameWidth, nYOffset + nFrameHeight);

                        // check to see if the collision box is contained within the frame
                        if (colBoxFrame.Contains(colBox.posMin) &&
                            colBoxFrame.Contains(colBox.posMax))
                        {
                            // it is contained, so add it to the frame's list of boxes
                            ColObj.GetCollisionFrames()[nFrameIndex].AddCollisionBox(colBox);
                        }

                        // update our frame x offset for the new column
                        nXOffset += nFrameWidth;

                        // update our frame index
                        ++nFrameIndex;
                    }

                    // update our frame offsets for the new row
                    nYOffset += nFrameHeight;
                    nXOffset = 0;
                }
            }

            // now save that fucker out! YAY!
            // Bob: "Yeah, that's right..." (with no emotion)
            // Bob (silently staring at me...  Like Amy "No Heart" Bendotti)
            // Bob (not silently belittling me)
            ColObj.Save(m_sBobFilename, nNumFrames);
        }

        private void loadCollisionInfoToolStripMenuItem_Click(object sender, EventArgs e)
        {
            OpenFileDialog fdlg = new OpenFileDialog();
            fdlg.Title = "Open Collision Info File Dialog";
            fdlg.InitialDirectory = @"c:\";
            fdlg.Filter = "BOB (*.bob)|*.bob";
            fdlg.FilterIndex = 1;
            fdlg.RestoreDirectory = true;
            if (fdlg.ShowDialog() == DialogResult.OK)
            {
                // create a new collision object and load it from the file name
                BumblePig.CollisionObject newColObj = new BumblePig.CollisionObject();
                newColObj.Load(fdlg.FileName);

                // clear out our current list of boxes
                m_lCollisionBoxes.Clear();

                // loop through the frames and push back all of the saved
                // collision boxes
                List<BumblePig.CollisionFrame> lFrames = newColObj.GetCollisionFrames();
                for (int nFrameIndex = 0; nFrameIndex < lFrames.Count; ++nFrameIndex)
                {
                    // loop through all of the boxes in the current frame
                    List<BumblePig.CollisionBox> lBoxes = lFrames[nFrameIndex].m_CollisionBoxes;
                    for (int nBoxIndex = 0; nBoxIndex < lBoxes.Count; ++nBoxIndex)
                    {
                        // add the current box to the global list of boxes
                        m_lCollisionBoxes.Add(lBoxes[nBoxIndex]);
                    }
                }

                // set our global saved box index
                // this restricts us to only undo-ing newly created boxes, not saved ones
                m_nLastSavedBoxIndex = m_lCollisionBoxes.Count;
            }
        }

        private void undoToolStripMenuItem_Click(object sender, EventArgs e)
        {
            // this guards us from undoing boxes that we have saved previously
            if (m_nLastSavedBoxIndex < m_lCollisionBoxes.Count)
            {
                // remove the last element
                m_lCollisionBoxes.RemoveAt(m_lCollisionBoxes.Count - 1);

                // invalidate the form so that it repaints
                Invalidate();
            }
        }
    }
}
